#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QRegularExpression>
#include <QClipboard>

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton, &QPushButton::released, this, &MainWindow::onBtnConvertReleased);
    connect(ui->btnCopy, &QPushButton::released, this, &MainWindow::onBtnCopyReleased);
    connect(ui->btnCopy2, &QPushButton::released, this, &MainWindow::onBtnCopy2Released);
    this->setFocusPolicy(Qt::ClickFocus);
    this->setWindowTitle("BLE 数据转换工具");
}

void MainWindow::onBtnConvertReleased()
{
    QString text = ui->lineEdit->text();
    qDebug() << text;
    QStringList nums;
    QRegularExpression re(R"(([0-9]+))");
    for(const QRegularExpressionMatch &match : re.globalMatch(text))
    {
        QString word = match.captured(1);
        nums << word;
    }
    qDebug() << nums;
    QStringList sl;
    for (auto& num : nums)
    {
        QChar cha = QChar(num.toInt());
        sl << cha;
    }
    QString result = sl.join("");
    qDebug() << result;
    ui->label->setText(result);

    QStringList sl2;
    for (auto& num : nums)
    {
        QString xVal = QString::number(num.toInt(), 16);
        sl2 << xVal;
    }
    qDebug() << sl2;
    ui->labelHex->setText(sl2.join(", "));
}


void MainWindow::onBtnCopyReleased()
{
    QClipboard *clip =  QApplication::clipboard();
    clip->setText(ui->label->text());
}

void MainWindow::onBtnCopy2Released()
{
    QClipboard *clip =  QApplication::clipboard();
    clip->setText(ui->labelHex->text());
}

MainWindow::~MainWindow()
{
    delete ui;
}

